package com.spi.soconversion.base.handlers;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;

import com.spi.soconversion.base.SOCException;
import com.spi.soconversion.util.DBConnection;
import com.spi.soconversion.util.ErrorConstants;
import com.spi.soconversion.util.ErrorMessageReader;
import com.spi.soconversion.util.QueryConstants;
import com.spi.soconversion.util.QueryReader;
import com.spi.soconversion.util.SOCConstants;
import com.spi.soconversion.util.SOCFileWriter;
import com.spi.soconversion.util.SOCUtility;

public class OpenSalesConversionStatisticsHandler {

	private String d_logFileName;
	private SOCFileWriter d_fileWriter;	
	private ArrayList<ConversionStatistics> messageList = new ArrayList<ConversionStatistics>();
	
	public OpenSalesConversionStatisticsHandler(String logFileName, SOCFileWriter fileWriter)
	{
		d_logFileName = logFileName;
		d_fileWriter = fileWriter;
	}
	
	public void addMessages(long orderNumber,int batchNum, String isConverted, String remarks,String LineId) throws SOCException
	{
		ConversionStatistics sce = new ConversionStatistics(orderNumber,batchNum,isConverted,remarks,LineId);
		
		if(messageList!=null && messageList.size()==SOCConstants.OPEN_SALES_CONVERSION_STATISTICS_UPDATE_FREQUENCY)
		{
			updateMessages();
			messageList = new ArrayList<ConversionStatistics>();
			messageList.add(sce);
		}
		else
		{
			messageList.add(sce);
		}
	}
	
	public void updateMessages() throws SOCException
	{
		boolean hasMessages= false;
		Iterator<ConversionStatistics> iter=messageList.iterator();
		if(iter.hasNext())
	    {
	    	d_fileWriter.write(d_logFileName, SOCUtility.writeLogMessage(SOCConstants.MESSAGE_PROCESSING_OPEN_ORDER_STATISTICS_STARTS));
	    	hasMessages=true;
	    }
		if(hasMessages)
		{
			String closedSalesStatisticsQuery = QueryReader.getQuery(QueryConstants.INSERT_OPEN_ORDER_CONVERSION_STATISTICS);
		
			PreparedStatement closedSalesStatisticsStmt = null;
			try {
				DBConnection.getConnection().setAutoCommit(false);
				closedSalesStatisticsStmt = DBConnection.getConnection().prepareStatement(closedSalesStatisticsQuery);
			} catch (SQLException se) {
				d_fileWriter.write(d_logFileName, SOCUtility.writeLogMessage(ErrorMessageReader.getError(
						ErrorConstants.FAILED_CREATE_STMT_OPEN_ORDER_CONVERSION_STATISTICS)+SOCConstants.NEW_LINE+se.getMessage()));
				throw new SOCException(
						ErrorMessageReader.getError(
								ErrorConstants.FAILED_CREATE_STMT_OPEN_ORDER_CONVERSION_STATISTICS),se);
			}	   
		   
		   
			while(iter.hasNext())
	    	{	
				ConversionStatistics  sce = iter.next();
				try {
					closedSalesStatisticsStmt.setLong(1,sce.getOrderNumber());
					closedSalesStatisticsStmt.setInt(2,sce.getBatchNum());
					closedSalesStatisticsStmt.setString(3,sce.getMessageType());
					closedSalesStatisticsStmt.setString(4,sce.getRemarks());
					closedSalesStatisticsStmt.setString(5,sce.getLineId());
					closedSalesStatisticsStmt.addBatch();
				} catch (SQLException se) {
					se.printStackTrace();
					d_fileWriter.write(d_logFileName, SOCUtility.writeLogMessage(ErrorMessageReader.getError(
							ErrorConstants.FAILED_ADDING_BATCH_OPEN_ORDER_CONVERSION_STATISTICS)+SOCConstants.NEW_LINE+se.getMessage()));
					throw new SOCException(
							ErrorMessageReader.getError(
									ErrorConstants.FAILED_ADDING_BATCH_OPEN_ORDER_CONVERSION_STATISTICS),se);
				}
				
	    	}
		    try {
				closedSalesStatisticsStmt.executeBatch();
				DBConnection.getConnection().commit();
				DBConnection.getConnection().setAutoCommit(true);
				 d_fileWriter.write(d_logFileName, SOCUtility.writeLogMessage(SOCConstants.MESSAGE_PROCESSING_OPEN_ORDER_STATISTICS_ENDS));
			} catch (SQLException se) {
				se.printStackTrace();
				d_fileWriter.write(d_logFileName, SOCUtility.writeLogMessage(ErrorMessageReader.getError(
						ErrorConstants.FAILED_UPDATE_BATCH_OPEN_ORDER_CONVERSION_STATISTICS)+SOCConstants.NEW_LINE+se.getMessage()));
				throw new SOCException(
						ErrorMessageReader.getError(
								ErrorConstants.FAILED_UPDATE_BATCH_OPEN_ORDER_CONVERSION_STATISTICS),se);
			}
		}
	}
}
